#!/bin/bash

# Redis Cache 快速测试脚本
BASE_URL="http://localhost:3000/api/v1"

echo "=========================================="
echo "    Redis Cache 功能测试"
echo "=========================================="
echo ""

# 检查服务是否运行
echo "1. 检查服务状态..."
if ! curl -s http://localhost:3000/api/v1/ > /dev/null; then
    echo "❌ 错误: 应用未运行，请先启动应用 (pnpm start:dev)"
    exit 1
fi
echo "✅ 应用运行正常"
echo ""

# 测试设置 token
echo "2. 测试设置 token..."
SET_RESULT=$(curl -s -X POST ${BASE_URL}/redis/set)
if [ "$SET_RESULT" = "OK" ]; then
    echo "✅ 设置成功: $SET_RESULT"
else
    echo "❌ 设置失败: $SET_RESULT"
fi
echo ""

# 等待一下
sleep 1

# 测试获取 token
echo "3. 测试获取 token..."
GET_RESULT=$(curl -s ${BASE_URL}/redis/get)
echo "📦 获取结果: $GET_RESULT"
if echo "$GET_RESULT" | grep -q "token"; then
    echo "✅ 获取成功"
else
    echo "❌ 获取失败或数据为空"
fi
echo ""

# 测试设置带过期时间的 token
echo "4. 测试设置带过期时间的 token..."
SETEX_RESULT=$(curl -s -X POST ${BASE_URL}/redis/setex)
if [ "$SETEX_RESULT" = "OK" ]; then
    echo "✅ 设置成功: $SETEX_RESULT"
else
    echo "❌ 设置失败: $SETEX_RESULT"
fi
echo ""

# 测试获取 TTL
echo "5. 测试获取 TTL..."
TTL_RESULT=$(curl -s ${BASE_URL}/redis/ttl)
echo "⏱️  TTL (毫秒): $TTL_RESULT"
if [ "$TTL_RESULT" != "null" ] && [ "$TTL_RESULT" != "-2" ] && [ "$TTL_RESULT" != "-1" ]; then
    echo "✅ TTL 获取成功"
else
    echo "⚠️  TTL 可能无效或键不存在"
fi
echo ""

# 最终验证
echo "6. 最终验证 - 获取所有 token..."
FINAL_RESULT=$(curl -s ${BASE_URL}/redis/get)
echo "📦 最终结果: $FINAL_RESULT"
echo ""

echo "=========================================="
echo "    测试完成"
echo "=========================================="

